package com.future;

import java.util.LinkedList;
import java.util.Queue;

/**
 * Description: 225. 用队列实现栈
 *
 * @author weiruibai.vendor
 * Date: 2022/11/9 17:18
 */
public class Solution_225 {


    /**
     * ["MyStack", "push", "push", "top", "pop", "empty"]
     * [[], [1], [2], [], [], []]
     */
    static MyStack obj = new MyStack();


    public static void main(String[] args) {
        obj.push(1);
        obj.push(2);
        System.out.println(obj.top());
        System.out.println(obj.pop());
        System.out.println(obj.empty());

    }

}


/**
 * 不能用Linkelist的peekFist pushLash pushFisrt等操作
 * 可以直接想象成这个队列只能从头进尾巴出
 * 只能用pop peek add isEmpty
 */
class MyStack {

    Queue<Integer> queue1;
    Queue<Integer> queue2;

    public MyStack() {
        queue1 = new LinkedList<>();
        queue2 = new LinkedList<>();
    }

    public void push(int x) {
        if (queue1.isEmpty() && queue2.isEmpty()) {
            queue1.add(x);
            return;
        } else if (queue1.isEmpty()) {
            queue1.add(x);
            while (!queue2.isEmpty()) {
                queue1.add(queue2.poll());
            }
        } else {
            queue2.add(x);
            while (!queue1.isEmpty()) {
                queue2.add(queue1.poll());
            }
        }
    }

    public int pop() {
        if (!queue1.isEmpty()) {
            return queue1.poll();
        } else {
            return queue2.poll();
        }
    }

    public int top() {
        if (!queue1.isEmpty()) {
            return queue1.peek();
        } else {
            return queue2.peek();
        }
    }

    public boolean empty() {
        return queue2.isEmpty() && queue1.isEmpty();
    }
}